package es.ats.codington.festival.services;

import java.sql.Connection;

import java.sql.SQLException;

import es.ats.codington.festival.daos.VisitorDAOImp;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.interfaces.services.IVisitorService;
import es.ats.codington.festival.utils.FERSDataConnection;

public class VisitorService implements IVisitorService{

	int nRows = 0;
	private Connection con;
	VisitorDAOImp visitorDAO = new VisitorDAOImp();
	
	
	
	public VisitorService() {
		super();
	}
	/**
	 * Methods for VisitorService class that checks whether the user is correct
	 * @return
	 */
	public int loginDAOService(String name, String pass) {
		try{
			Visitor visitor= new Visitor();
			visitor.setUserName(name);
			visitor.setPassword(pass);
			con = FERSDataConnection.getConnection();
			nRows = visitorDAO.loginDAO(visitor, con);
			con.close();
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return nRows;
	}
	/**
	 * SETTER methods for VisitorService class 
	 * @return
	 */
	public int insertVisitorService(Visitor visitor){
		try{
			con = FERSDataConnection.getConnection();
			nRows = visitorDAO.insertVisitor(visitor, con);
			con.close();
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return nRows;
	}
	/**
	 * SETTER methods for VisitorService class 
	 * @return
	 */
	public int updateVisitorSevice(Visitor uVisitor , String userName){
		try{
			con = FERSDataConnection.getConnection();
			nRows = visitorDAO.updateVisitor(uVisitor, userName,con);
			con.close();
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return nRows;
	}
	/**
	 * SETTER methods for VisitorService class 
	 * @return
	 */
	public int deleteVisitorService(String visitor){
		try{
			con = FERSDataConnection.getConnection();
			nRows = visitorDAO.deleteVisitor(visitor, con);
			con.close();
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return nRows;
	}
	/**
	 * SETTER methods for VisitorService class 
	 * @return
	 */
	public int updatePasswordService(String newPassword, String oldPassword, String userName) {
		try{
			con = FERSDataConnection.getConnection();
			nRows = visitorDAO.updatePassword(newPassword, oldPassword, userName, con);
			con.close();
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return nRows;
	}
	/**
	 * GETTER methods for VisitorService class 
	 * @return
	 */
	public int getVisitorIdService(String userName) {
		try{
			con = FERSDataConnection.getConnection();
			nRows = visitorDAO.getVisitorId(userName, con);
			con.close();
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return nRows;
	}
	/**
	 * GETTER methods for VisitorService class 
	 * @return
	 */
	public int isAdminService (String userName) {
		int resultado=0;
		try{
			con = FERSDataConnection.getConnection();
			resultado = visitorDAO.isAdmin(userName, con);
			con.close();
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return resultado;
	}
	public Visitor getVisitorService(String userName) {
		Visitor visitor = new Visitor();
		try{
			con = FERSDataConnection.getConnection();
			visitor = visitorDAO.getVisitor(userName, con);
			con.close();
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return visitor;
	}
}
